package thread;

public class Demo13 {
    public static void main1(String[] args) {
        //线程的状态是一个枚举类型 Thread.State
        //来看看线程的状态：
        for(Thread.State state : Thread.State.values()) {
            System.out.println(state);
        }
    }

    public static void main2(String[] args) throws InterruptedException {
        //我们来看看每一个状态是在什么样的就情况下出现的
        //NEW : 安排了新工作但是还没有开始行动
        Thread t = new Thread(() -> {
            System.out.println("hello thread");
        });

        System.out.println(t.getState());

        t.start();

        Thread.sleep(1000);
        System.out.println("main 结束");
    }

    public static void main3(String[] args) throws InterruptedException {
        //TERMINATED : ⼯作完成了.
        Thread t = new Thread(() -> {
            //这里的构造是 lambda 表达式的重写run方法，覆盖Thread的run方法
            System.out.println("hello thread");

        });

        t.start();

        Thread.sleep(1000);
        System.out.println(t.getState());
    }

    public static void main4(String[] args) throws InterruptedException {
        Thread t = new Thread(() -> {
           //RUNNABLE : 可⼯作的.⼜可以分成正在⼯作中和即将开始⼯作.
            while(true) {
                //什么也不做，让其一直在t线程
            }
        });
        t.start();

        Thread.sleep(1000);
        System.out.println(t.getState());
    }

    public static void main5(String[] args) throws InterruptedException {
        Thread t = new Thread(() -> {
            //TIMED_WAITING:这⼏个都表⽰排队等着其他事情
            while(true) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });

        t.start();

        Thread.sleep(1000);
        System.out.println(t.getState());
    }

    public static void main(String[] args) throws InterruptedException {
        //另外对于 join(时间) 也可以引起 TIMED_WAITING 状态
        //这个需要用 jconsloe 进行调试，才可以看到这个状态
        Thread t = new Thread(() -> {
           while(true) {
               System.out.println("hello thread");
               try {
                   Thread.sleep(1000);
               } catch (InterruptedException e) {
                   throw new RuntimeException(e);
               }
           }
        });
        t.start();

        t.join(6000 * 1000);
    }

    public static void main7(String[] args) throws InterruptedException {
        Thread t = new Thread(() -> {
           //WAITING : 这⼏个都表⽰排队等着其他事情
           //使用 join() 方式引起 WAITING 状态,就是死等
            System.out.println("hello thread");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
        t.start();

        t.join();
    }
}
